/**
 * Copyright (c) 2012, Camelot and/or its affiliates. All rights reserved.
 * Camelot PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.camelot.ccr.system.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import com.camelot.ccr.global.dao.genericimpl.GenericHibernateDaoImpl;
import com.camelot.ccr.global.util.Pager;
import com.camelot.ccr.global.util.Utility;
import com.camelot.ccr.system.controller.DriverController;
import com.camelot.ccr.system.dao.DriverDao;
import com.camelot.ccr.system.persistence.Driver;

/**
 * <pre>
 * 车手
 * </pre>
 * @author Liu Jun
 * @version V0.1, 2014-5-20 下午10:30:05
 */
@Repository
public class DriverDaoImpl extends GenericHibernateDaoImpl<Driver, String> implements DriverDao {

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.DriverDao#findPageList(int, int, com.camelot.ccr.system.persistence.Driver)
	 */
	@Override
	public Pager<Driver> findPageList(int curpage, int pageSize, Driver example) {
		StringBuilder sb = new StringBuilder("from Driver d where 1=1  ");
		List<Object> parameList = new ArrayList<Object>();
		if(Utility.isNotNull(example.getTeam())&&Utility.isNotNull(example.getTeam().getId())){
			sb.append(" and d.team.id = ? ");
			parameList.add(example.getTeam().getId());
		}
		if(Utility.isNotEmpty(example.getDriverName())){
			sb.append(" and d.driverName like ? ");
			parameList.add("%"+example.getDriverName()+"%");
		}
		return (Pager<Driver>) queryListPageByHql(sb.toString(), curpage, pageSize, parameList);
	}

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.DriverDao#saveDriver(com.camelot.ccr.system.persistence.Driver)
	 */
	@Override
	public void saveDriver(Driver driver,MultipartFile file) throws IllegalStateException, IOException {
		String logoRealPathDir = driver.getHeadPhoto();
		String photoPath = Utility.uploadFile(file, logoRealPathDir,DriverController.headUrlPath);
		driver.setHeadPhoto(photoPath);
		save(driver);
	}

	/* (non-Javadoc)
	 * @see com.camelot.ccr.system.dao.DriverDao#randGetList()
	 */
	@Override
	public List<Driver> randGetList() {
		String hql = "from Driver d order by d.id ";
		return queryListPage(hql, 0, 6);
	}
	
}
